#ifndef WEBCONFIG_H
#define WEBCONFIG_H

#include <ESP8266WebServer.h>
#include "Config.h"

class WebConfig {
private:
  ESP8266WebServer server;
  Config* configPtr;
  
public:
  WebConfig(Config* config) : server(80), configPtr(config) {}
  
  void begin() {
    server.on("/", HTTP_GET, [&]() {
      String html = R"(
        <!DOCTYPE html>
        <html>
        <head>
          <title>ESP8266 SHT30 配置</title>
          <meta charset="UTF-8">
          <style>
            body { font-family: Arial, sans-serif; margin: 40px; }
            .container { max-width: 600px; margin: 0 auto; }
            .form-group { margin-bottom: 15px; }
            label { display: block; margin-bottom: 5px; }
            input[type="text"], input[type="password"], input[type="number"] {
              width: 100%; padding: 8px; box-sizing: border-box;
            }
            button { padding: 10px 20px; background: #4CAF50; color: white; border: none; }
          </style>
        </head>
        <body>
          <div class="container">
            <h1>ESP8266 SHT30 配置</h1>
            <form action="/update" method="post">
              <div class="form-group">
                <label for="server">MQTT服务器:</label>
                <input type="text" id="server" name="server" value=")" + String(configPtr->mqttServer) + R"(">
              </div>
              <div class="form-group">
                <label for="port">MQTT端口:</label>
                <input type="number" id="port" name="port" value=")" + String(configPtr->mqttPort) + R"(">
              </div>
              <div class="form-group">
                <label for="user">MQTT用户名:</label>
                <input type="text" id="user" name="user" value=")" + String(configPtr->mqttUser) + R"(">
              </div>
              <div class="form-group">
                <label for="password">MQTT密码:</label>
                <input type="password" id="password" name="password" value=")" + String(configPtr->mqttPassword) + R"(">
              </div>
              <div class="form-group">
                <label for="topic">MQTT主题:</label>
                <input type="text" id="topic" name="topic" value=")" + String(configPtr->mqttTopic) + R"(">
              </div>
              <button type="submit">保存配置</button>
            </form>
          </div>
        </body>
        </html>
      )";
      server.send(200, "text/html", html);
    });
    
    server.on("/update", HTTP_POST, [&]() {
      if (server.hasArg("server")) strcpy(configPtr->mqttServer, server.arg("server").c_str());
      if (server.hasArg("port")) configPtr->mqttPort = server.arg("port").toInt();
      if (server.hasArg("user")) strcpy(configPtr->mqttUser, server.arg("user").c_str());
      if (server.hasArg("password")) strcpy(configPtr->mqttPassword, server.arg("password").c_str());
      if (server.hasArg("topic")) strcpy(configPtr->mqttTopic, server.arg("topic").c_str());
      
      saveConfig(*configPtr);
      
      server.send(200, "text/html", "<script>alert('配置已保存'); window.location.href='/';</script>");
    });
    
    server.begin();
    Serial.println("HTTP服务器已启动");
  }
  
  void handleClient() {
    server.handleClient();
  }
};

#endif